//
//  SZLWineFriendsCircelController.m
//  Shengzhuli
//
//  Created by 卡忙科技 on 16/12/15.
//  Copyright © 2016年 kamangkeji. All rights reserved.
//

#import "SZLWineFriendsCircelController.h"
#import "SZLAccountHelper.h"
#import "WineFriendsNetwork.h"
#import "MBProgressHUD+MJ.h"
#import "CirContentCell.h"
#import "SZLPublicCirController.h"

@interface SZLWineFriendsCircelController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,strong) UITableView *sourceTab;
@property (nonatomic,strong) NSMutableArray *sources;

@end

@implementation SZLWineFriendsCircelController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"酒友圈";
    [self setNavi];
    [self.view addSubview:self.sourceTab];
    [self getCirContent];
}
- (void)setNavi
{
    UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 50, 30)];
    [btn setTitle:@"发布" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(publicClick) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:btn];
}
- (void)publicClick
{
    SZLPublicCirController *publicVc = [[SZLPublicCirController alloc]init];
    __WEAK
    publicVc.publicSuccess = ^(){
        [weakSelf.sources removeAllObjects];
        [weakSelf getCirContent];
    };
    [self.navigationController pushViewController:publicVc animated:YES];
}
- (void)getCirContent
{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"token"] = [SZLAccountHelper shareInstance].token;
    __WEAK
    [self showWaiter];
    [WineFriendsNetwork getWineFriendsCirListWithParame:params response:^(BOOL success, NSString *msg, NSArray *sources) {
        [self hideWaiter];
        if (success) {
            if (sources.count == 0) {
                [weakSelf showNoDataView:self.view.bounds];
            }else{
                [weakSelf.sources addObjectsFromArray:sources];
                [weakSelf.sourceTab reloadData];
            }
        }else{
            [MBProgressHUD showError:msg toView:self.view];
        }
    }];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CirContentCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([CirContentCell class])];
    return [cell getCellHeightWithModel:self.sources[indexPath.section]];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.sources.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CirContentCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([CirContentCell class])];
    cell.model = self.sources[indexPath.section];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
- (UITableView *)sourceTab
{
    if (!_sourceTab) {
        _sourceTab = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, ScreenH-64) style:UITableViewStyleGrouped];
        _sourceTab.delegate = self;
        _sourceTab.dataSource = self;
        //去掉group顶部间距
        _sourceTab.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, 0.01)];
        _sourceTab.sectionHeaderHeight = 0.01;
        _sourceTab.sectionFooterHeight = 10;
        _sourceTab.backgroundColor = RGBColorOne(240);
        
        _sourceTab.tableFooterView = [[UIView alloc]init];
        [_sourceTab registerNib:[UINib nibWithNibName:NSStringFromClass([CirContentCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([CirContentCell class])];
    }
    return _sourceTab;
}
- (NSMutableArray *)sources
{
    if (!_sources) {
        _sources = [NSMutableArray array];
    }
    return _sources;
}

@end
